package com.wavehi.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;

/**
 * @Author WaveHi
 * @Date 27/9/2021 下午 5:02
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description Filter for page user_index.jsp
 */
@WebFilter(
        initParams = {
        @WebInitParam(name = "filter_name", value = "myFilter")
},
        dispatcherTypes={DispatcherType.REQUEST, DispatcherType.INCLUDE
},
urlPatterns = "/*"
)
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //The user of filterConfig
        //Get the filter name
        System.out.println("Filter name is :  " + filterConfig.getFilterName());
        //Get the specified value of init parameter
        System.out.println("Init parameter is :" + filterConfig.getInitParameter("filter_name"));
        //Get the Enumeration that contains all the name of init parameter
        System.out.println("Enumeration : " + filterConfig.getInitParameterNames());
        //Get the servlet context
        System.out.println("ServletContext : " + filterConfig.getServletContext());
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("I am login filter.");
        //United the character set
        servletRequest.setCharacterEncoding("UTF-8");
        servletResponse.setContentType("UTF-8");
        //Let request go
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}
